﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Maticsoft.Common;
namespace Web.Controllers
{
    public class AdminController : BaseController
    {
        Maticsoft.Model.T_Adm M_user = new Maticsoft.Model.T_Adm();
        //
        // GET: /Admin/
        #region setViewData
        public void setViewData()
        {
                Maticsoft.BLL.T_Adm bll = new Maticsoft.BLL.T_Adm();
                string id = Session["UserInfo"].ToString();
                M_user = bll.GetModel(id);
                ViewData["UserName"] = M_user.Name;
        }

        public void setViewData(bool set)
        {
            if(set)
            {  Maticsoft.BLL.T_Adm bll = new Maticsoft.BLL.T_Adm();
            string id = Session["UserInfo"].ToString();
            M_user = bll.GetModel(id);
            ViewData["UserName"] = M_user.Name;
            ViewData["UserPassword"] = M_user.Password;
            ViewData["UserId"] = M_user.Id;
            ViewData["UserSex"] = M_user.Sex.Trim();
            ViewData["UserOld"] = M_user.Old;
            ViewData["UserEmail"] = M_user.Email;
            ViewData["UserTel"] = M_user.Tel;
            ViewData["UserPhoto"] = M_user.Photo;
            ViewData["UserQuestion1"] = M_user.PassQuestion1;
            ViewData["UserQuestion1"] = M_user.PassQuestion2;
            ViewData["UserQuestion1"] = M_user.PassQuestion3;
            ViewData["UserAsk1"] = M_user.PassAsk1;
            ViewData["UserAsk2"] = M_user.PassAsk2;
            ViewData["UserAsk3"] = M_user.PassAsk3;
            }
            else
            {
                Maticsoft.BLL.T_Adm bll = new Maticsoft.BLL.T_Adm();
                string id = Session["UserInfo"].ToString();
                M_user = bll.GetModel(id);
                ViewData["UserName"] = M_user.Name;
                ViewData["UserNum"] = M_user.Name;
                ViewData["TalkNum"] = M_user.Name;
                ViewData["DoctorNum"] = M_user.Name;
                ViewData["UserNum"] = M_user.Name;
            }
        }

        
        #endregion

        #region 首页
        public ActionResult Index()
        {
            setViewData();

            return View();
        }
        #endregion

        #region  医生
        public ActionResult Widgets()
        {
            setViewData();
            return View();
        }
        #endregion

        #region 用户
        public ActionResult Charts()
        {
            setViewData();
            return View();
        }
        #endregion

        #region 监护数据
        public ActionResult Tables()
        {
            setViewData();
            return View();
        }
        #endregion

        #region 管理员
        public ActionResult Forms()
        {
            setViewData();
            return View();
        }
        #endregion

        #region 操作记录
        public ActionResult Panels()
        {
            setViewData();
            return View();
        }
        #endregion

        #region 个人资料
        public ActionResult Person()
        {
            setViewData(true);
            return View();
        }
        #endregion




        #region 获取Json_Doctor
        public JsonResult Json_Doctor()
        {
            Maticsoft.BLL.T_Doctor bll = new Maticsoft.BLL.T_Doctor();
            List<Maticsoft.Model.T_Doctor> doctors = bll.GetModelList("");
            var res = new JsonResult();
            res.Data = doctors;
            res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return res;
        }
        #endregion

        #region 获取Json_User
        public JsonResult Json_User()
        {
            Maticsoft.BLL.T_User bll = new Maticsoft.BLL.T_User();
            List<Maticsoft.Model.T_User> users = bll.GetModelList("");
            var res = new JsonResult();
            res.Data = users;
            res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return res;
        }
        #endregion

        #region 获取Json_Admin
        public JsonResult Json_Admin()
        {
            Maticsoft.BLL.T_Adm bll = new Maticsoft.BLL.T_Adm();
            List<Maticsoft.Model.T_Adm> doctors = bll.GetModelList("");
            var res = new JsonResult();
            res.Data = doctors;
            res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return res;
        }
        #endregion

        #region 获取Json_Monitor
        public JsonResult Json_Monitor()
        {
            Maticsoft.BLL.T_Monitor bll = new Maticsoft.BLL.T_Monitor();
            Maticsoft.BLL.T_User use = new Maticsoft.BLL.T_User();
            List<Maticsoft.Model.T_Monitor> monitors = bll.GetModelList("NowTime='*'");
            var res = new JsonResult();
            res.Data = monitors;
            res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return res;
        }
        #endregion



        #region 查询获得Json
        public JsonResult GetJson()
        {
            string classs = Request["class"];
            string id = (Request["id"]);
            if (classs.Trim() == "doctor")
            {
                Maticsoft.BLL.T_Doctor bll = new Maticsoft.BLL.T_Doctor();
                Maticsoft.Model.T_Doctor doctor = bll.GetModelByCache(id);
                var res = new JsonResult();
                res.Data = doctor;
                res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                return res;
            }
            else if (classs.Trim() == "user")
            {
                Maticsoft.BLL.T_User bll = new Maticsoft.BLL.T_User();
                Maticsoft.Model.T_User user = bll.GetModelByCache(id);
                var res = new JsonResult();
                res.Data = user;
                res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                return res;

            }
            else if (classs.Trim() == "monitor")
            {
                Maticsoft.BLL.T_Monitor bll = new Maticsoft.BLL.T_Monitor();
                Maticsoft.Model.T_Monitor monitor = bll.GetModelByCache(id);
                var res = new JsonResult();
                res.Data = monitor;
                res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                return res;

            }
            else if (classs.Trim() == "admin")
            {
                Maticsoft.BLL.T_Adm bll = new Maticsoft.BLL.T_Adm();
                Maticsoft.Model.T_Adm admin = bll.GetModelByCache(id);
                var res = new JsonResult();
                res.Data = admin;
                res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                return res;

            }
            return null;
        }
        #endregion

        #region 修改数据
        public JsonResult SetJson()
        {
            string classs = Request["class"];
            #region 医生类的设置
            if (classs.Trim() == "doctor")
            {
                Maticsoft.BLL.T_Doctor bll = new Maticsoft.BLL.T_Doctor();
                string name = Request["name"];
                string sex = Request["sex"];
                string id = Request["id"];
                int old = int.Parse(Request["old"]);
                string tel = Request["tel"];
                string email = Request["email"];
                long offices = 6;
                string office = Request["offices"];
                switch (office)
                {
                    case "内科": offices = 3; break;
                    case "外科": offices = 4; break;
                    case "妇产科": offices = 5; break;
                    default
                    : break;
                }
                Maticsoft.Model.T_Doctor doctor = bll.GetModelByCache(id);
                doctor.Name = name;
                doctor.Email = email;
                doctor.Old = old;
                doctor.Sex = sex;
                doctor.Tel = tel;
                doctor.Offices = offices;
                bll.Update(doctor);
                return Json_Doctor();
            }
            #endregion
            #region 用户类的设置
            else if (classs.Trim() == "user")
            {
                Maticsoft.BLL.T_User bll = new Maticsoft.BLL.T_User();
                string name = Request["name"];
                string sex = Request["sex"];
                string id = Request["id"];
                int old = int.Parse(Request["old"]);
                string tel = Request["tel"];
                string email = Request["email"];
                long offices = 6;
                string office = Request["offices"];
                string weigh = Request["weigh"];
                int height = int.Parse(Request["height"]);
                string illness = Request["illness"];
                string time = Request["time"];
                switch (office)
                {
                    case "内科": offices = 3; break;
                    case "外科": offices = 4; break;
                    case "妇产科": offices = 5; break;
                    default
                    : break;
                }
                Maticsoft.Model.T_User user = bll.GetModelByCache(id);
                user.Time_Str = time;
                user.Name = name;
                user.Email = email;
                user.Old = old;
                user.Sex = sex;
                user.Tel = tel;
                user.Offices = offices;
                user.Height = height;
                user.Weight = weigh;
                user.Illness = illness;
                bll.Update(user);
                return Json_User();
            }
            #endregion
            #region 数据类的设置
            else if (classs.Trim() == "monitor")
            {
                return Json_Monitor();
            }
            #endregion
            #region 管理员类的设置
            else if (classs.Trim() == "admin")
            {
                Maticsoft.BLL.T_Adm bll = new Maticsoft.BLL.T_Adm();
                string name = Request["name"];
                string sex = Request["sex"];
                string id = Request["id"];
                int old = int.Parse(Request["old"]);
                string tel = Request["tel"];
                string email = Request["email"];

                Maticsoft.Model.T_Adm admin = bll.GetModelByCache(id);
                admin.Name = name;
                admin.Email = email;
                admin.Old = old;
                admin.Sex = sex;
                admin.Tel = tel;
               if( bll.Update(admin))
                return Json_Admin();
            }
            #endregion
            return null;
        }
        #endregion

        #region 删除数据
        public JsonResult RemoveJson()
        {
            string classs = Request["class"];
            #region 医生类的删除
            if (classs.Trim() == "doctor")
            {
                Maticsoft.BLL.T_Doctor bll = new Maticsoft.BLL.T_Doctor();
                string id = Request["id"];
                bll.Delete(id);
                return Json_Doctor();
            }
            #endregion
            #region 用户类的删除
            else if (classs.Trim() == "user")
            {
                Maticsoft.BLL.T_User bll = new Maticsoft.BLL.T_User();
                string id = Request["id"];
                bll.Delete(id);
                return Json_User();
            }
            #endregion
            #region 数据类的删除
            else if (classs.Trim() == "monitor")
            {
                Maticsoft.BLL.T_Monitor bll = new Maticsoft.BLL.T_Monitor();
                string id = Request["id"];
                bll.Delete(id);
                return Json_Monitor();
            }
            #endregion
            #region 管理员类的删除
            else if (classs.Trim() == "admin")
            {
                Maticsoft.BLL.T_Adm bll = new Maticsoft.BLL.T_Adm();
                string id = Request["id"];
                bll.Delete(id);
                return Json_Admin();
            }
            #endregion

            return null;
        }
        #endregion

        #region 添加数据
        public JsonResult AddJson()
        {

            string classs = Request["class"];

            #region 医生类的添加
            if (classs.Trim() == "doctor")
            {
                Maticsoft.BLL.T_Doctor bll = new Maticsoft.BLL.T_Doctor();
                string name = Request["name"];
                string sex = Request["sex"];
                int old = int.Parse(Request["old"]);
                string tel = Request["tel"];
                string email = Request["email"];
                long offices = 6;
                string office = Request["offices"];
                switch (office)
                {
                    case "内科": offices = 3; break;
                    case "外科": offices = 4; break;
                    case "妇产科": offices = 5; break;
                    default
                    : break;
                }
                Maticsoft.Model.T_Doctor doctor = new Maticsoft.Model.T_Doctor();
                doctor.Name = name;
                doctor.Email = email;
                doctor.Old = old;
                doctor.Sex = sex;
                doctor.Tel = tel;
                doctor.Offices = offices;
                bll.Add(doctor);
                return Json_Doctor();
            }
            #endregion
            #region 用户类的添加
            else if (classs.Trim() == "user")
            {
                Maticsoft.BLL.T_User bll = new Maticsoft.BLL.T_User();
                string name = Request["name"];
                string sex = Request["sex"]; ;
                int old = int.Parse(Request["old"]);
                string tel = Request["tel"];
                string email = Request["email"];
                long offices = 6;
                string office = Request["offices"];
                string time = Request["time"];

                string illness = Request["illness"];
                switch (office)
                {
                    case "内科": offices = 3; break;
                    case "外科": offices = 4; break;
                    case "妇产科": offices = 5; break;
                    default
                    : break;
                }
                Maticsoft.Model.T_User user = new Maticsoft.Model.T_User();
                user.Time_Str = time;
                user.Name = name;
                user.Email = email;
                user.Old = old;
                user.Sex = sex;
                user.Tel = tel;
                user.Offices = offices;
                string high = Request["height"];
                if (!String.IsNullOrEmpty(high))
                {
                    int? height = 0;// int.Parse(Request["height"]);
                    user.Height = height;
                }
                string weigh = Request["weigh"];
                if (!String.IsNullOrEmpty(weigh))
                {
                    user.Weight = weigh;
                }
                user.Illness = (String.IsNullOrEmpty(illness)) ? user.Illness : illness;
                if (bll.Add(user))
                    return Json_User();
            }
            #endregion
            #region 数据类的添加
            else if (classs.Trim() == "monitor")
            {
                return Json_Monitor();
            }
            #endregion
            #region 管理员类的添加
            else if (classs.Trim() == "admin")
            {
                Maticsoft.BLL.T_Adm bll = new Maticsoft.BLL.T_Adm();
                string name = Request["name"];
                string sex = Request["sex"];
                int old = int.Parse(Request["old"]);
                string tel = Request["tel"];
                string email = Request["email"]; 
                string question1 = Request["question1"];
                string question2 = Request["question2"];
                string question3 = Request["question3"];
                Maticsoft.Model.T_Adm admin = new Maticsoft.Model.T_Adm();
                admin.Name = name;
                admin.Email = email;
                admin.Old = old;
                admin.Sex = sex;
                admin.Tel = tel;
                admin.PassQuestion1 = question1;
                admin.PassQuestion2 = question2;
                admin.PassQuestion3 = question3;
                if(bll.Add(admin))
                  return Json_Admin();
            }
            #endregion

            return null;
        }
        #endregion

        public ActionResult setIndex()
        {

            return null;
        }
    }
}
